package core.java;




import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 *
 */
class Exinterfacelmpl2 {

    public void execu() {
        System.out.println("Exinterfacelmpl");
    }
}

/**
 *
 */
public class CGLIBProxy implements MethodInterceptor {
    private Exinterfacelmpl2 target;
    public CGLIBProxy(Exinterfacelmpl2 target){
        this.target=target;
    }
    public Exinterfacelmpl2 createCGLIBProxy(){
       Enhancer enhancer=new Enhancer();
       enhancer.setSuperclass(target.getClass());
       enhancer.setCallback(this);
       return (Exinterfacelmpl2)enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if (method.getName().equals("execu")){
            System.out.println("start ....");
            try {
                try {
                    methodProxy.invokeSuper(o,null);
                }finally {
                    System.out.println(".... end");
                }
                System.out.println("success");
            }catch (Throwable e){
                System.out.println("Throwable");
            }
        }
        return null;
    }
    public static void main(String[] args){
        Exinterfacelmpl2 exinterfacelmpl=new Exinterfacelmpl2();
        exinterfacelmpl=new CGLIBProxy(exinterfacelmpl).createCGLIBProxy();
        exinterfacelmpl.execu();
    }
}
